'''

Copyright 2010 Manuel Reinelt

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Created on 12.04.2010

@author: Manuel Reinelt
contact: mrdevlab@googlemail.com

'''

from optparse import OptionParser
import os
import sys

class parse_cl_input(object):
    '''
    parse cli with python optparse-module
    '''

    def __init__(self):
        '''
        Constructor
        '''
        
        self.options = ""
        self.args = ""
        
        desc = """encrpyt and decrypts files, <c> 2010 Manuel Reinelt, mrdevlab@googlemail.com"""
        
        parser = OptionParser(description=desc,version="%prog version=0.0.0.")
        parser.add_option("-e","--encrypt",help="encrypt with key, -e or -d is mandatory",dest="ekey")
        parser.add_option("-d","--decrypt",help="decrypt with key, -e or -d is mandatory",dest="dkey")
        parser.add_option("-c","--cipher",help="enc/decrypt with cipher (aes or blowfish), default is [%default]",default="blowfish",dest="cipher")
        parser.add_option("-i","--input",help="specify source file (mandatory)",dest="inputfile")
        parser.add_option("-o","--output",help="specify destination file (default: input.crypted)",dest="outputfile")
        parser.add_option("-w","--wipe",help="wipe sourcefile, 1=fill with 0, 2=random, 3=random aes cipher",dest="wipe")
        parser.add_option("-v","--verbose",help="set verbose",dest="verbose",default=False, action="store_true")
               
        (self.options, self.args) = parser.parse_args()
        self.parser = parser
              
        self.validate()
    
    def validate(self):       
        
        if(self.options.verbose):
            print "\n" + "validating passed arguments"
        
        if self.options.dkey:
            self.encdec = "dec"
            self.key = self.options.dkey
        elif self.options.ekey: 
            self.encdec = "enc"
            self.key = self.options.ekey
        else:
            print "ERROR: no key, exiting"
            sys.exit(-1)
           
        if len(self.key) > 56:
            print "ERROR: max. keylength is 56 characters, exiting"
            sys.exit(-1)
  
        if self.options.inputfile is None:
            print "ERROR: no inputfile, exiting"
            sys.exit(-1)
        elif self.options.outputfile is None:
            if os.path.splitext(self.options.inputfile)[1]==".enc":
                #remove the .enc extension on encrypted files:
                self.options.outputfile = os.path.splitext(self.options.inputfile)[0]
            else:
                #all other files (encrypted without ".enc" and unencrypted files get)
                #appropriate extension:
                self.options.outputfile = self.options.inputfile + "." + self.encdec
       
        if self.options.cipher == "aes":
            if [16,24,32].count(len(self.key)) <> 1:
                print "ERROR: AES key must be either 16, 24, or 32 bytes long"
                sys.exit(-1)
        elif not self.options.cipher == "blowfish":
            print "ERROR: unsupported cipher (supported: blowfish, aes), exiting"
            sys.exit(-1)
     
    

        
               
        
       
       
        
        
            
        
        
        
        

        